using System;
using System.Collections.Generic;
using System.Linq;
using HZYProject.Domain;
using Microsoft.AspNetCore.Builder;
//using Microsoft.AspNetCore.Builder;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;

namespace HZYProject.Data
{
    /// <summary>
    /// 数据初始化
    /// </summary>
    public static class DataInitializer
    {
        public static IApplicationBuilder UseDataInitializer(
            this IApplicationBuilder builder)
        {
            using (var scope = builder.ApplicationServices.CreateScope())
            {
                var dbcontext = scope.ServiceProvider.GetService<HZYProjectDbContext>();

                System.Console.WriteLine("开始执行迁移数据库...");
                dbcontext.Database.Migrate();
                System.Console.WriteLine("数据库迁移完成...");


                #region 用户的初始化数据
                #region 院校数据
                var collegeid = Guid.NewGuid();

                var colleges = new List<Colleges> {
                    new Colleges
                    {
                        CollegesID = collegeid,
                        Name = "河质"
                    },
                    new Colleges
                    {
                        CollegesID = Guid.NewGuid(),
                        Name = "新职"
                    } ,
                    new Colleges
                    {
                        CollegesID = Guid.NewGuid(),
                        Name = "河工大"
                    },
                    new Colleges
                    {
                        CollegesID = Guid.NewGuid(),
                        Name = "河工贸"
                    },
                    new Colleges
                    {
                        CollegesID = Guid.NewGuid(),
                        Name = "周职"
                    } ,
                    new Colleges
                    {
                        CollegesID = Guid.NewGuid(),
                        Name = "驻职"
                    },
                    new Colleges
                    {
                        CollegesID = Guid.NewGuid(),
                        Name = "安职"
                    }

                };

                foreach (var item in colleges)
                {
                    dbcontext.Colleges.Add(item);
                }
                #endregion

                #region 班级数据
                Class c = new Class()
                {
                    ClassID = Guid.NewGuid(),
                    CollegesID = collegeid,
                    CreateTime = DateTime.Now
                    ,
                    Name = "默认班级",
                    Status = 1
                };
                dbcontext.Classes.Add(c);
                #endregion

                #endregion




                if (dbcontext.Users.Any())
                {
                    return builder; // 数据已经初始化了。
                }

                var users = new[] {
                    new User { UserId=Guid.NewGuid(), UserName="admin", PassWord="admin", CreateTime=DateTime.Now, Status=1
                        , CollegesID=collegeid,RealName="张三", Sex="男"
                    }

                };
                foreach (User item in users)
                {
                    dbcontext.Users.Add(item);
                }

                dbcontext.SaveChanges();



            }

            return builder;
        }
    }
}